/**
 * @addtogroup brd_crypto_def
 * brd_crypto_def
 * @{
 */
/**
 * @file brd_crypto_def.h
 * @brief 板级层密码定义
 * @note
 * @par 修改日志
 * <table>
 * <tr><th>Date         <th>Version     <th>Author      <th>Description
 * <tr><td>2024-09-03   <td>1.0         <td>软件工程院  <td>创建初始版本
 * @copyright 杭州鸿泉物联网技术股份有限公司
 */
#ifndef _BRD_CRYPTO_DEF_H_
#define _BRD_CRYPTO_DEF_H_

#ifdef __cplusplus
extern "C" {
#endif

/// 加密芯片类型
enum Brd_TCryptoIdTag {
    kBrd_CryptoIdBase                           = 0,
    kBrd_CryptoIdCiu98b                         = kBrd_CryptoIdBase,    ///< CIU98B
    kBrd_CryptoIdHse                            = 1,                    ///< S32K3内部HSE模块
    kBrd_CryptoIdMax,
};
typedef enum Brd_TCryptoIdTag Brd_TCryptoId;

#define BRD_CRYPTO_ID_CIU98B
#define BRD_CRYPTO_ID_HSE

/**************************************** CIU98B *************************************/
/// CIU98B支持的对称算法
enum Brd_TCryptoCiu98bSymTag {
    kBrd_CryptoCiu98bSymBase                    = 0,
    kBrd_CryptoCiu98bSymMax                     = kBrd_CryptoCiu98bSymBase,
};
typedef enum Brd_TCryptoCiu98bSymTag Brd_TCryptoCiu98bSym;

/// CIU98B支持的非对称算法
enum Brd_TCryptoCiu98bAsymTag {
    kBrd_CryptoCiu98bAsymBase                   = 0,
    kBrd_CryptoCiu98bAsymSm2                    = kBrd_CryptoCiu98bAsymBase,      ///< 国密SM2
    kBrd_CryptoCiu98bAsymMax,
};
typedef enum Brd_TCryptoCiu98bAsymTag Brd_TCryptoCiu98bAsym;

#define BRD_CRYPTO_CIU98B_ASYM_SM2

/// CIU98B支持的摘要算法
enum Brd_TCryptoCiu98bDigestTag {
    kBrd_CryptoCiu98bDigestBase                 = 0,
    kBrd_CryptoCiu98bDigestMax                  = kBrd_CryptoCiu98bDigestBase,
};
typedef enum Brd_TCryptoCiu98bDigestTag Brd_TCryptoCiu98bDigest;

/**************************************** HSE *************************************/
/// HSE支持的所有算法
enum Brd_TCryptoHseAlgoTag {
    /// 对称算法
    kBrd_CryptoHseAlgoSymBase                       = 0,
    kBrd_CryptoHseAlgoSymAes                        = kBrd_CryptoHseAlgoSymBase,        ///< AES, 支持128, 192, 256位
    kBrd_CryptoHseAlgoSymHmac                       = 1,                                ///< HMAC, 最大1024bits
    kBrd_CryptoHseAlgoSymMax,

    /// 非对称算法
    kBrd_CryptoHseAlgoAsymBase                      = kBrd_CryptoHseAlgoSymMax + 1, 
    kBrd_CryptoHseAlgoAsymRsa                       = kBrd_CryptoHseAlgoAsymBase,       ///< RSA, 支持1024, 2048, 3072, 4096位
    kBrd_CryptoHseAlgoAsymMax,

    /// 摘要算法
    kBrd_CryptoHseAlgoHashBase                      = kBrd_CryptoHseAlgoAsymMax + 1,
    kBrd_CryptoHseAlgoHashSha_1                     = kBrd_CryptoHseAlgoHashBase + 2,   ///< SHA1
    kBrd_CryptoHseAlgoHashSha2_224,         ///< SHA2_224
    kBrd_CryptoHseAlgoHashSha2_256,         ///< SHA2_256
    kBrd_CryptoHseAlgoHashSha2_384,         ///< SHA2_256
    kBrd_CryptoHseAlgoHashSha2_512,         ///< SHA512
    kBrd_CryptoHseAlgoHashSha2_512_224,     ///< SHA512
    kBrd_CryptoHseAlgoHashSha2_512_256,     ///< SHA512
    kBrd_CryptoHseAlgoHashSha3_224,         ///< SHA512
    kBrd_CryptoHseAlgoHashSha3_256,         ///< SHA512
    kBrd_CryptoHseAlgoHashSha3_384,         ///< SHA512
    kBrd_CryptoHseAlgoHashSha3_512,         ///< SHA512
    kBrd_CryptoHseAlgoHashMp,               ///< Miyaguchi-Preneel compression using AES-ECB with 128-bit key size (SHE spec support).
    kBrd_CryptoHseAlgoHashMax,
};
typedef enum Brd_TCryptoHseAlgoTag Brd_TCryptoHseAlgo;

#define BRD_CRYPTO_HSE_ALGO_SYM_AES
#define BRD_CRYPTO_HSE_ALGO_SYM_HMAC
#define BRD_CRYPTO_HSE_ALGO_ASYM_RSA
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA_1
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA2_224
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA2_256
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA2_384
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA2_512
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA2_512_224
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA2_512_256
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA3_224
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA3_256
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA3_384
#define BRD_CRYPTO_HSE_ALGO_HASH_SHA3_512
#define BRD_CRYPTO_HSE_ALGO_HASH_MP

/*********** AES算法相关结构 *************/
/// AES密钥索引
enum 
{
    kBrd_CryptoHseAesKeyIndex0   = 0,          ///< AES128
    kBrd_CryptoHseAesKeyIndex1   = 1,          ///< AES128
    kBrd_CryptoHseAesKeyIndex2   = 2,          ///< AES128
    kBrd_CryptoHseAesKeyIndex3   = 3,          ///< AES128
    kBrd_CryptoHseAesKeyIndex4   = 4,          ///< AES128、192、256
    kBrd_CryptoHseAesKeyIndex5   = 5,          ///< AES128、192、256
    kBrd_CryptoHseAesKeyIndex6   = 6,          ///< AES128、192、256
    kBrd_CryptoHseAesKeyIndex7   = 7,          ///< AES128、192、256
    kBrd_CryptoHseAesKeyIndexMax = 8,
};

/// AES加密模式
typedef enum Brd_TCryptoHseAesBlockModeTag Brd_TCryptoHseAesBlockMode;
enum Brd_TCryptoHseAesBlockModeTag
{
    kBrd_CryptoHseAesBlockModeCTR        = (UInt8)(1U << 1U),    ///< CTR 模式
    kBrd_CryptoHseAesBlockModeCBC        = (UInt8)(1U << 2U),    ///< CBC 模式
    kBrd_CryptoHseAesBlockModeECB        = (UInt8)(1U << 3U),    ///< ECB 模式
    kBrd_CryptoHseAesBlockModeCFB        = (UInt8)(1U << 4U),    ///< CFB 模式
    kBrd_CryptoHseAesBlockModeOFB        = (UInt8)(1U << 5U),    ///< OFB 模式
};

/// AES密钥结构
struct Brd_TCryptoHseAesKeyCustomTag
{
    UInt32 key_index;                           ///< 密钥索引, @input
    UInt32 key_size;                            ///< 密钥bit长度, 对于产生和导入密钥为@input, 对于导出为@input/output; 支持128, 192, 256
    UInt8* key;                                 ///< 密钥, 对于产生密钥不需要, 对于导入密钥为@input, 对于导出为@output;
    Brd_TCryptoHseAesBlockMode block_mode;      ///< 加密模式, @input
};
typedef struct Brd_TCryptoHseAesKeyCustomTag Brd_TCryptoHseAesKeyCustom;

/// AES加密/解密需要的参数
struct Brd_TCryptoHseAesCryptCustomTag
{
    UInt32 key_index;                           ///< 密钥索引, @input
    UInt32 input_len;                           ///< 输入数据长度, @input, 单位: 字节, 不支持填充
    UInt8* input;                               ///< 输入数据, @input
    UInt8* output;                              ///< 输出数据, @output
    const UInt8* iv;                            ///< 初始化向量, @input, ECB模式不需要
    Brd_TCryptoHseAesBlockMode block_mode;      ///< 加密模式, @input
};
typedef struct Brd_TCryptoHseAesCryptCustomTag Brd_TCryptoHseAesCryptCustom;

/*********** RSA算法相关结构 *************/
/// RSA密钥索引
enum 
{
    kBrd_CryptoHseRsaKeyIndex0   = 0,          ///< 自身私钥对
    kBrd_CryptoHseRsaKeyIndex1   = 1,          ///< 别人的公钥
    kBrd_CryptoHseRsaKeyIndexMax = 2,
};

///  RSA加密算法
typedef enum {
    kBrd_CryptoHseRsaAlgoCryptNopadding      = ((UInt8)0x90U),   ///< The input will be treated as an unsigned integer and perform a modular exponentiation of the input
    kBrd_CryptoHseRsaAlgoCryptRsaesOaep      = ((UInt8)0x91U),   ///< RSAES OAEP cipher
    kBrd_CryptoHseRsaAlgoCryptRsaesPkcs1v15  = ((UInt8)0x92U),   ///< ECDSA RSAES_PKCS1_V15 cipher
} Brd_TCryptoHseRsaCryptAlgo;

///  RSA签名算法
typedef enum {
    kBrd_CryptoHseRsaSignAlgoSsaPkcs1v15     = ((UInt8)0x93U),   ///< RSASSA_PKCS1_V15 signature scheme
} Brd_TCryptoHseRsaSignAlgo;

/// RSA产生或者导入/导出密钥需要的参数
/// 指数请用UInt8数组传递，并明确数组长度，主要防止指数中存在多余字节; 
/// 错误示例: UInt32 exp=1; exp_len=4，这样不允许，
/// 正确示例: UInt8 exp[1]={1}; exp_len=1
typedef struct Brd_TCryptoHseRsaKeyCustomTag Brd_TCryptoHseRsaKeyCustom;
struct Brd_TCryptoHseRsaKeyCustomTag
{
    UInt32 key_index;                           ///< 密钥索引, @input
    UInt32 key_size;                            ///< 密钥bit长度; 对于产生和导入密钥为@input, 对于导出为@input/output; 支持1024, 2048, 3072, 4096
    UInt8* mod;                                 ///< 模数, 产生时为@output, 如果为NULL, 则不输出; 导入密钥时为@input; 导出时为@output; 模数长度和密钥长度一致, mod大小>=密钥长度=key_size/8
    UInt8* exp;                                 ///< 指数, 对于产生和导入密钥为@input, 对于导出为@output;
    UInt32 exp_len;                             ///< 指数长度, 对于产生和导入密钥为@input, 对于导出为@input/output;
};

/// RSA 加密/解密方案
typedef struct
{
    Brd_TCryptoHseRsaCryptAlgo algo;   ///< INPUT: RSA 算法
    /** INPUT: 只有RSA-OAEP需要下列参数*/
    UInt32  hash_algo;      ///< INPUT: The Hash algorithm for RSA OAEP padding. 参见 Brd_TCryptoHseAlgo 中的HASH算法
    UInt32  label_length;   ///< INPUT: Optional OAEP label length (it can be 0). Must be less than 128
    UInt8*  plabel;         ///< INPUT: Optional OAEP label (it can be NULL if label length is 0). Must be less than 128 bytes long
} Brd_TCryptoHseRsaCryptScheme;

/// RSA签名方案
typedef struct
{
    Brd_TCryptoHseRsaSignAlgo algo;  ///< INPUT: RSA 算法
    UInt32 hash_algo;   ///< INPUT: The Hash algorithm for RSA OAEP padding. 参见 Brd_TCryptoHseAlgo 中的HASH算法
} Brd_TCryptoHseRsaSignScheme;

/// RSA加密/解密参数
typedef struct Brd_TCryptoHseRsaCryptCustomTag Brd_TCryptoHseRsaCryptCustom;
struct Brd_TCryptoHseRsaCryptCustomTag
{
    UInt32 key_index;                           ///< 密钥索引, @input
    Brd_TCryptoHseRsaCryptScheme scheme;        ///< RSA加密方案, @input
    UInt8* input;                               ///< 输入数据, @input
    UInt32 input_len;                           ///< 输入数据长度, @input
    UInt8* output;                              ///< 输出数据, @output
    UInt32 output_len;                          ///< 输出数据长度, @inout/output
};

/// RSA签名/验签参数
typedef struct Brd_TCryptoHseRsaSignCustomTag Brd_TCryptoHseRsaSignCustom;
struct Brd_TCryptoHseRsaSignCustomTag
{
    UInt32 key_index;                           ///< 密钥索引, @input
    Brd_TCryptoHseRsaSignScheme scheme;         ///< RSA签名方案, @input
    UInt8* input;                               ///< 密文, @input
    UInt32 input_len;                           ///< 输入数据长度, @input
    UInt8* sign;                                ///< 签名, 签名时为@output, 验签时为@input
    UInt32 sign_len;                            ///< 签名长度, 签名时为@input/output, 验签时为@input
};

/*********** HMAC算法相关结构 *************/
/// HMAC密钥索引
enum 
{
    kBrd_CryptoHseHmacKeyIndex0   = 0,          ///< 最大1024bits
    kBrd_CryptoHseHmacKeyIndex1   = 1,          ///< 最大1024bits
    kBrd_CryptoHseHmacKeyIndexMax = 2,
};

/// HMAC密钥结构
struct Brd_TCryptoHseHmacKeyCustomTag
{
    UInt32 key_index;                           ///< 密钥索引, @input
    UInt32 key_size;                            ///< 密钥bit长度, 对于产生和导入密钥为@input, 对于导出为@input/output;
    UInt8* key;                                 ///< 密钥, 对于产生密钥不需要, 对于导入密钥为@input, 对于导出为@output;
};
typedef struct Brd_TCryptoHseHmacKeyCustomTag Brd_TCryptoHseHmacKeyCustom;

/// HMAC计算需要的参数
struct Brd_TCryptoHseHmacSignCustomTag
{
    UInt32 key_index;                           ///< 密钥索引, @input
    UInt8* input;                               ///< 输入数据, @input
    UInt32 input_len;                           ///< 输入数据长度, 单位: 字节, 不支持填充, @input
    UInt8* sign;                                ///< mac, 计算mac时为@output, 验证时为@input
    UInt32 sign_len;                            ///< mac长度, 计算mac时为@input/output, 验签时为@input
    UInt32 hash_algo;                           ///< 哈希算法, @input. 参见 Brd_TCryptoHseAlgo 中的HASH算法
};
typedef struct Brd_TCryptoHseHmacSignCustomTag Brd_TCryptoHseHmacSignCustom;

#ifdef __cplusplus
}  ///  < extern "C"
#endif

#endif // _BRD_CRYPTO_DEF_H_
/**
 * @} 
 */
